#pragma once
#include "TextOutput.h"
#include <varargs.h>

class CLogger : public ITextOutput
{
protected:

	HANDLE m_hOut;
	
public:
	CLogger(void)
	{
		::FreeConsole();
		::AllocConsole();
		m_hOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
		BOOL r1 = ::SetConsoleCtrlHandler(NULL, TRUE);
		BOOL r2 = ::SetConsoleCtrlHandler(&CLogger::HandlerRoutine, TRUE);
	}
	virtual ~CLogger(void)
	{
		m_hOut = NULL;
		::FreeConsole();
	}

protected:

	static BOOL WINAPI HandlerRoutine(DWORD dwCtrlType)
	{
		::FreeConsole();
		return TRUE;
	}


public:

	virtual void printf(const char * szFormat, ...)
	{
		CStringA str;
		va_list argList;
		va_start(argList, szFormat);
		str.FormatV(szFormat, argList);
		va_end(argList);
		const CStringW out(str);

		DWORD nWritten = 0;
		::WriteConsoleW(m_hOut, out.GetString(), out.GetLength(), &nWritten, NULL);
	}

	virtual void printf(const wchar_t * szFormat, ...)
	{
		CStringW out;
		va_list argList;
		va_start(argList, szFormat);
		out.FormatV(szFormat, argList);
		va_end(argList);

		DWORD nWritten = 0;
		::WriteConsoleW(m_hOut, out.GetString(), out.GetLength(), &nWritten, NULL);
	}
};



class CDummyLogger : public ITextOutput
{
public:
	virtual void printf(const char * szFormat, ...) {}
	virtual void printf(const wchar_t * szFormat, ...) {}
};
